package com.apobates.jforum2.troll.nest;

import com.apobates.jforum2.troll.nest.websocket.MessageSocketPushHandler;
import com.apobates.jforum2.troll.nest.websocket.TopicSocketUpdatedHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置文件类
 *
 * @author xiaofanku
 * @since 20200527
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(topicUpdatedHandler(), "/sock/updated").withSockJS();
        registry.addHandler(messageUpdatedHandler(), "/sock/message", "").withSockJS();
    }

    //WebSocket
    @Bean(name = "topicUpdatedHandler")
    public TopicSocketUpdatedHandler topicUpdatedHandler() {
        return new TopicSocketUpdatedHandler();
    }

    @Bean(name = "messageUpdatedHandler")
    public MessageSocketPushHandler messageUpdatedHandler() {
        return new MessageSocketPushHandler();
    }
}
